package com.navinfo.tripanalysis.common.pojo;

import lombok.Data;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.catalyst.expressions.GenericRow;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.Metadata;
import org.apache.spark.sql.types.StructField;
import org.apache.spark.sql.types.StructType;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 0F39故障信息
 */
@Data
public class Point0F39 implements Serializable,Cloneable {

    /**
     * 车辆通讯号
     */
    private String tid;
    /**
     * 协议类型，0F39
     */
    private String protocol;
    /**
     * 时间,单位：s 从1970-01-01 00:00:00开始所经过的秒数
     */
    private String gpsTime;
    /**
     * 84原始经度，单位度，扩展10的6次方
     */
    private String originallng;
    /**
     * 84原始纬度，单位度，扩展10的6次方
     */
    private String originallat;
    /**
     * 02经度，单位度，扩展10的6次方
     */
    private String longitude;
    /**
     * 02纬度，单位度，扩展10的6次方
     */
    private String latitude;
    /**
     * 高程，海拔高度，单位m
     */
    private String height;
    /**
     * 方向(0~359)正北为0，顺时针
     */
    private String direction;
    /**
     * 车速，单位KM/H
     */
    private String speed;
//    /**
//     * 故障明细信息
//     */
//    private List<FaultInfo> faultInfo;
    /**
     * 故障类型，1：产生，2：消失
     */
    private int faultType;
    /**
     * 车速,扩大100倍
     */
    private int subSpeed;
    /**
     * 油门开度百分比，单位%，扩展100倍
     */
    private int accelerator;
    /**
     * 制动信号状态，0关，1开，扩展100倍
     */
    private int brake;
    /**
     * 发动机转速，单位rpm转每分钟，扩展100倍
     */
    private int rotation;
    /**
     * 发动机涡轮增压压力，扩展100倍
     */
    private int engineTurbochargedPressure;
    /**
     * 进气压力，扩展100倍
     */
    private int engineIntakePressure;
    /**
     * 发动机排气温度，扩展100倍
     */
    private  int engineExhaustTemperature;
    /**
     * 发动机水温，扩展100倍
     */
    private  int engineCoolantTemperature;
    /**
     * 油门变化率，扩展100倍
     */
    private  int accelerationRate;
    /**
     * 挡位，扩展100倍
     */
    private  int gear;
    /**
     * 发动机输出扭矩，扩大100倍
     */
    private  int engineOutputTorque;
    /**
     * 载荷，扩大100倍
     */
    private  long load;
    /**
     * 发动机负荷，扩大100倍
     */
    private  int engineLoad;
    /**
     * 车辆加速度，扩大100倍
     */
    private  int vehicleAcceleration;
    /**
     * 车辆减速度，扩大100倍
     */
    private  int vehicleSpeedReduction;
    /**
     * 故障码列表
     */
    private  int faultCodeCount;
    /**
     * 源地址，故障码属于哪个系统
     */
    private int faultAddr;
    /**
     * 故障码，业务端匹配
     */
    private int spn;
    /**
     * 故障码，业务端匹配
     */
    private int fmi;

    public static StructType createSchema(){
        List<StructField> list = new ArrayList<>();
        list.add(new StructField("tid", DataTypes.StringType,true, Metadata.empty()));
        list.add(new StructField("protocol", DataTypes.StringType,true, Metadata.empty()));
        list.add(new StructField("gpsTime", DataTypes.StringType,true, Metadata.empty()));
        list.add(new StructField("originallng", DataTypes.StringType,true, Metadata.empty()));
        list.add(new StructField("originallat", DataTypes.StringType,true, Metadata.empty()));
        list.add(new StructField("longitude", DataTypes.StringType,true, Metadata.empty()));
        list.add(new StructField("latitude", DataTypes.StringType,true, Metadata.empty()));
        list.add(new StructField("height", DataTypes.StringType,true, Metadata.empty()));
        list.add(new StructField("direction", DataTypes.StringType,true, Metadata.empty()));
        list.add(new StructField("speed", DataTypes.StringType,true, Metadata.empty()));
        list.add(new StructField("faultAddr", DataTypes.IntegerType,true, Metadata.empty()));
        list.add(new StructField("spn", DataTypes.IntegerType,true, Metadata.empty()));
        list.add(new StructField("fmi", DataTypes.IntegerType,true, Metadata.empty()));
        list.add(new StructField("faultType", DataTypes.IntegerType,true, Metadata.empty()));
        list.add(new StructField("subSpeed", DataTypes.IntegerType,true, Metadata.empty()));
        list.add(new StructField("accelerator", DataTypes.IntegerType,true, Metadata.empty()));
        list.add(new StructField("brake", DataTypes.IntegerType,true, Metadata.empty()));
        list.add(new StructField("rotation", DataTypes.IntegerType,true, Metadata.empty()));
        list.add(new StructField("engineTurbochargedPressure", DataTypes.IntegerType,true, Metadata.empty()));
        list.add(new StructField("engineIntakePressure", DataTypes.IntegerType,true, Metadata.empty()));
        list.add(new StructField("engineExhaustTemperature", DataTypes.IntegerType,true, Metadata.empty()));
        list.add(new StructField("engineCoolantTemperature", DataTypes.IntegerType,true, Metadata.empty()));
        list.add(new StructField("accelerationRate", DataTypes.IntegerType,true, Metadata.empty()));
        list.add(new StructField("gear", DataTypes.IntegerType,true, Metadata.empty()));
        list.add(new StructField("engineOutputTorque", DataTypes.IntegerType,true, Metadata.empty()));
        list.add(new StructField("load", DataTypes.LongType,true, Metadata.empty()));
        list.add(new StructField("engineLoad", DataTypes.IntegerType,true, Metadata.empty()));
        list.add(new StructField("vehicleAcceleration", DataTypes.IntegerType,true, Metadata.empty()));
        list.add(new StructField("vehicleSpeedReduction", DataTypes.IntegerType,true, Metadata.empty()));
        list.add(new StructField("faultCodeCount", DataTypes.IntegerType,true, Metadata.empty()));

        return new StructType(list.toArray(new StructField[list.size()]));
    }

    public static Row createRow (Point0F39 p){
        List<Object> list = new ArrayList<>();
        list.add(p.getTid());
        list.add(p.getProtocol());
        list.add(p.getGpsTime());
        list.add(p.getOriginallng());
        list.add(p.getOriginallat());
        list.add(p.getLongitude());
        list.add(p.getLatitude());
        list.add(p.getHeight());
        list.add(p.getDirection());
        list.add(p.getSpeed());
        list.add(p.getFaultAddr());
        list.add(p.getSpn());
        list.add(p.getFmi());
        list.add(p.getFaultType());
        list.add(p.getSubSpeed());
        list.add(p.getAccelerator());
        list.add(p.getBrake());
        list.add(p.getRotation());
        list.add(p.getEngineTurbochargedPressure());
        list.add(p.getEngineIntakePressure());
        list.add(p.getEngineExhaustTemperature());
        list.add(p.getEngineCoolantTemperature());
        list.add(p.getAccelerationRate());
        list.add(p.getGear());
        list.add(p.getEngineOutputTorque());
        list.add(p.getLoad());
        list.add(p.getEngineLoad());
        list.add(p.getVehicleAcceleration());
        list.add(p.getVehicleSpeedReduction());
        list.add(p.getFaultCodeCount());

        return new GenericRow(list.toArray());
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
